#include <stdlib.h>
#include "../lib/cutest/CuTest.h"
#include "../include/map.h"
#include "../include/mine_map.h"

void TestCreateMap(CuTest *tc) {
    map* Map = CreateMap(MODE_EASY);
    CuAssertIntEquals_Msg(tc, "X size in easy mode", MX_EASY, Map -> mx);
    CuAssertIntEquals_Msg(tc, "Y size in easy mode", MY_EASY, Map -> my);
    CuAssertIntEquals_Msg(tc, "cnt in easy mode", NUM_EASY, Map -> cnt);
    CuAssertIntEquals_Msg(tc, "mode in easy mode", MODE_EASY, Map -> mode);
    CuAssertIntEquals_Msg(tc, "generated in easy mode", 0, Map -> generated);
    Map = CreateMap(MODE_MID);
    CuAssertIntEquals_Msg(tc, "X size in mid mode", MX_MID, Map -> mx);
    CuAssertIntEquals_Msg(tc, "Y size in mid mode", MY_MID, Map -> my);
    CuAssertIntEquals_Msg(tc, "cnt in mid mode", NUM_MID, Map -> cnt);
    CuAssertIntEquals_Msg(tc, "mode in mid mode", MODE_MID, Map -> mode);
    CuAssertIntEquals_Msg(tc, "generated in mid mode", 0, Map -> generated);
    Map = CreateMap(MODE_HARD);
    CuAssertIntEquals_Msg(tc, "X size in hard mode", MX_HARD, Map -> mx);
    CuAssertIntEquals_Msg(tc, "Y size in hard mode", MY_HARD, Map -> my);
    CuAssertIntEquals_Msg(tc, "cnt in hard mode", NUM_HARD, Map -> cnt);
    CuAssertIntEquals_Msg(tc, "mode in hard mode", MODE_HARD, Map -> mode);
    CuAssertIntEquals_Msg(tc, "generated in hard mode", 0, Map -> generated);
}

void TestSetMine(CuTest *tc) {
    srand((int)time(NULL)); 
    for(int testCases=0;testCases<30;testCases++) {
        map* Map = CreateMap(testCases / 10 + 1); // 10 * [easy, mid, hard]
        int x = rand() % Map -> mx + 1, y = rand() % Map -> my + 1;
        Setmine(Map, x, y, rand());
        for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++)
            CuAssertIntEquals_Msg(tc, "no mine around first click pos", 0, Map -> mineMap[x + i][y + j]);
        int cnt = 0;
        for(int i=1;i<=Map->mx;i++) for(int j=1;j<=Map->my;j++)
            cnt += Map -> mineMap[i][j];
        CuAssertIntEquals_Msg(tc, "cnt in the map", Map -> cnt, cnt);
    }
}

CuSuite* MapGetSuite() {
    CuSuite* suite = CuSuiteNew();
    SUITE_ADD_TEST(suite, TestCreateMap);
    SUITE_ADD_TEST(suite, TestSetMine);
    return suite;
}